package com.woniuxy.JDKproxy.cglib;


import com.woniuxy.JDKproxy.aspect.Aspect;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 我们的CGLib类，用来动态生成子类，通过子类重写目标父类的方法，来实现增强扩展方法
 */
public class CgLibProxy {
    public Object getSunProxy(Object target){
        Object sun = Enhancer.create(target.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

                Aspect aspect = new Aspect();
                aspect.log();

                Object sunObject = method.invoke(target.getClass().newInstance(),objects);


                aspect.checkPermission();
                return sunObject;
            }
        });
        return sun;
    }
}
